package org.xiaojl.xcloud.tools.gateway.kit;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xiaojl.xcloud.tools.gateway.RpcResult;
import org.xiaojl.xcloud.tools.gateway.accesslog.AccessLogDBKit;
import org.xiaojl.xcloud.tools.gateway.accesslog.AccessLogEntity;

import com.netflix.zuul.context.RequestContext;

/**  
 * <p>Title: FilterKit</p>  
 *
 * <p>Description: 过滤器工具类</p>  
 *
 * @author jilong.xiao  
 * @date 2018年4月23日  
 */
public class FilterKit {
	private static final Logger log = LoggerFactory.getLogger(FilterKit.class);
	/**
	 * 过滤器成功标识
	 */
	public static final String FILTER_SUCCESS_FLAG = "isSuccess";
	
	/**  
	 * <p>Title: setRespBody</p>  
	 * <p>Description: 设置Filter的返回内容</p>  
	 * @param context filter上下文
	 * @param rpcResult  响应结果对象
	 */ 
	public static void setRespBody(RequestContext context, RpcResult rpcResult) {
		FilterKit.setRespBody(context,JsonKit.toJson(rpcResult));
	}
	
	/**
	 * <p>Title: setRespBody</p>  
	 * <p>Description: 设置Filter的返回内容</p>  
	 * @param context filter上下文
	 * @param responseBody 响应结果字符串
	 */
	public static void setRespBody(RequestContext context, String responseBody) {
		//解决中文乱码的问题
		HttpServletResponse response = context.getResponse();
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		//设置返回内容  
		context.setResponseBody(responseBody);
		System.err.println("最终响应结果："+responseBody);
		
		//存储访问日志
		saveAccassLog(context, responseBody);
	}
	
	public static void saveAccassLog(RequestContext context, String returnData){
		HttpServletRequest request = context.getRequest();
		String serviceId = context.get("serviceId")==null?"":context.get("serviceId")+"";
		String uri = request.getRequestURI();
		//获取所有的请求参数
		Map<String, String[]> parameterMap = request.getParameterMap();
		String params = JsonKit.toJson(parameterMap);
		log.info("\r\n\tMethod:\t{}\r\n\tPath:\t{}\r\n\tParameters:\t{}\r\n\tResponse:\t{}", request.getMethod(), uri, params, returnData);
		
		//构建访问日志
		AccessLogEntity entity = new AccessLogEntity();
		entity.setCrtTime(new Date());
		entity.setCrtHost(IpKit.getRequestIp(request));
		entity.setUri(uri);
		entity.setSerId(serviceId);
		entity.setOpt(request.getMethod());
		entity.setCrtResult(returnData );
		
		//存储访问日志
		AccessLogDBKit.getInstance().offerQueue(entity);
	}
	
	/**
	 * <p>Title: setFilterFlag</p>  
	 * <p>Description: 设置过滤器是否通过的标识：通过则设置为true，未通过则设置为false</p>  
	 * @param context
	 */
	public static void setFilterFlag(RequestContext context, Boolean flag){
		context.set(FILTER_SUCCESS_FLAG, flag);  
	}
	
	/**
	 * <p>Title: isFilterFlag</p>  
	 * <p>Description: 获取上一个过滤器是否通过的标识</p>  
	 * @param context
	 * @return true表示通过，false表示未通过
	 */
	public static boolean isFilterFlag(RequestContext context){
		Object obj = context.get(FILTER_SUCCESS_FLAG);
		if(obj == null){
			return true;
		}
		return (Boolean) obj;
	}
}
